package com.example.administrator.runbook.fragments.community.utils;

import android.os.Handler;
import android.os.Message;

import com.jakewharton.disklrucache.DiskLruCache;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.JsonHttpResponseHandler;

import org.json.JSONObject;

import java.io.BufferedWriter;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.SocketTimeoutException;

import cz.msebera.android.httpclient.Header;

/**
 * Created by Administrator on 2016/11/8.
 */

public class AsyncHttpClientHelper {

    private static final String TAG = "123456";
    private static final AsyncHttpClientHelper helper = new AsyncHttpClientHelper();
    private static final AsyncHttpClient client = new AsyncHttpClient();

    public static AsyncHttpClientHelper getInstance() {
        return helper;
    }

    public void get(final Handler handler, final String url) {
        client.get(url, new JsonHttpResponseHandler() {

            @Override
            public void onSuccess(int statusCode, Header[] headers, final JSONObject response) {

                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        String md5 = DiskCache.getMD5(url);
                        DiskLruCache cache = DiskCache.getDiskLruCache("json");
                        try {
                            DiskLruCache.Editor edit = cache.edit(md5);
                            OutputStream os = edit.newOutputStream(0);
                            OutputStreamWriter osw = new OutputStreamWriter(os, "utf-8");
                            BufferedWriter writer = new BufferedWriter(osw);
                            writer.write(response.toString());
                            writer.close();
                            edit.commit();
                            cache.flush();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }).start();

                Message message = handler.obtainMessage(0);
                message.obj = response;
                handler.sendMessage(message);
                Log.i("success");
                android.util.Log.i(TAG, "onSuccess: ");
            }


            @Override
            public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONObject errorResponse) {
//                super.onFailure(statusCode, headers, throwable, errorResponse);
                failure(handler, throwable);
            }


        });
    }

    public void getComment(final Handler handler, String url) {
        client.get(url, new JsonHttpResponseHandler() {

            @Override
            public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                Message message = handler.obtainMessage(10);
                message.obj = response;
                handler.sendMessage(message);
                Log.i("success");
                android.util.Log.i(TAG, "onSuccess: ");
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONObject errorResponse) {
                failure(handler, throwable);
            }
        });
    }

    private void failure(Handler handler, Throwable throwable) {
        if (throwable instanceof SocketTimeoutException) {
            Message message = handler.obtainMessage(1);
            message.obj = "网络开小差了";
            handler.sendMessage(message);
        } else if (throwable instanceof Exception) {
            Message message = handler.obtainMessage(1);
            message.obj = "网络开小差了";
            handler.sendMessage(message);
        }
    }
}
